/*
 * test_others.c
 *
 *  Created on: 12/05/2013
 *      Author: jose
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../tests.h"

struct list_st
{
	int i;
	struct list_st * next;
};
typedef struct list_st List;

unsigned empty=0;

typedef struct generic_adj_list_st Generic_Adj_List;

struct generic_adj_list_st
{
	void * key;
	void * nodes;

	//Functions
	void (*create_key)(Generic_Adj_List *, void *);
	void * (*search)(Generic_Adj_List *);
	void (*print)(Generic_Adj_List *);
};



Generic_Adj_List *
create_generic_list(unsigned len)
{
	Generic_Adj_List * l = (Generic_Adj_List *) calloc(len, sizeof(Generic_Adj_List));
	if (!l)
		return NULL;
	return l;
}

void
create_key_unsigned(Generic_Adj_List * self, void * key_value)
{
	self->key = (unsigned *) calloc(1, sizeof(unsigned));
	if (!self->key)
		return;

	*(unsigned*)self->key = *(unsigned*)key_value;
	empty++;
}

void
print_key_unsigned(Generic_Adj_List * self)
{
	printf("\nKey %u", *(unsigned *)self->key);
}

void
create_key_string(Generic_Adj_List * self, void * key_value)
{
	char * str = (char *)key_value;
	unsigned len = strlen(str);

	self->key = (char *) calloc(len+1, sizeof(char));
	if (!self->key)
		return;

	memcpy(self->key, str, len * sizeof(char));
	empty++;
}

void
print_key_string(Generic_Adj_List * self)
{
	printf("\nKey %s", (char *)self->key);
}


void
test_generic_adj_list2(){
	Generic_Adj_List * list = NULL;

	unsigned i;

	list = (Generic_Adj_List *) calloc (1, sizeof(Generic_Adj_List));
	if (!list)
		return;

	list->create_key = &create_key_unsigned;
	list->print = &print_key_unsigned;

	i=333;
	list->create_key(list, &i);

	list->print(list);

}




void
test_generic_adj_list(){
	Generic_Adj_List * list = NULL;

	unsigned i;

	list = (Generic_Adj_List *) calloc (4, sizeof(Generic_Adj_List));
	if (!list)
		return;

	for(i = 0 ; i<4 ; i++)
	{
		if (i&1)
		{
			list[i].create_key = &create_key_unsigned;
			list[i].print = &print_key_unsigned;
			list[i].create_key(&list[i], &i);
		}
		else
		{
			list[i].create_key = &create_key_string;
			list[i].print = &print_key_string;
			list[i].create_key(&list[i], "cadenakey");
		}
	}


	for(i = 0 ; i<4 ; i++)
		list[i].print(&list[i]);
}




void
test_list()
{
	List * l = NULL; /* inicio de la lista */
	List * l_temp = NULL, * l_ptr = NULL;
	int i;

	for (i=0 ; i < 5 ; i++)
	{
		l_temp = (List *) calloc(1, sizeof(List));
		if (!l_temp)
			return;

		l_temp->i = i;

		if (l)
		{
			for (l_ptr=l ; l_ptr->next ; l_ptr=l_ptr->next);
			l_ptr->next = l_temp;
		}
		else
			l = l_temp;

		l_temp = NULL;

	}

	for (l_ptr=l ; l_ptr ; l_ptr=l_ptr->next)
		printf("\n%d", l_ptr->i);


}
